#!/bin/zsh
#定义基础变量 Start
export COMPILE_MODE_KEY="compileMode"
export COMPILE_MODE_DEBUG="debug"
export COMPILE_MODE_RELEASE="release"
#定义基础变量 End



# 判断哪些平台需要编译.
# 不编译
export FLAG_NO="n"
# 编译
export FLAG_YES="y"
# 判断当前平台是不是该平台,是就编译否则不编译.
export BUILD_FLAG_AUTO="A"
platformArray=("ios" "android" "mac" "linux")

### User Configure End ###

# +++ 脚本常量 Start  +++ bnm,m
export COMPLIE_ACTION_KEY="action"
export COMPILE_ACTION_CONFIGURE="c"
export COMPILE_ACTION_MAKE="m"
export COMPILE_ACTION_IGNORE="i"
export COMPILE_ACTION_CONFIGURE_MAKE="b"
# === 脚本常量 End  ===

# === Lib Build Flag===

# +++ Variable Start +++
# 全局地址
export g_projectDir=$(
    cd $(dirname $0)
    pwd
)
export g_inputRootDir=${g_projectDir}/01-input
if [[ ! -e ${g_inputRootDir} ]]; then
    mkdir -p ${g_inputRootDir}
fi

# -s 找到返回0 没找到返回1 windows上没有-s参数.
# 没有找到返回1,找到返回0.
# which git 2>/dev/null 1>&2
# echo $?
# [[ $? == 1 ]] && echo "yq not installed" 
# exit

# [[ $? == 1 ]] && echo "yq not installed" && brew install yq


export g_configFile=$g_projectDir/config.json
export g_compileMode=$(eval jq -r '.$COMPILE_MODE_KEY' $g_configFile)
echo "compile mode  is: $g_compileMode"
export SCRITP_ROOT_DIR=${g_projectDir}/02-script
export g_buildRootDir=${g_projectDir}/03-build
export g_outputRootDir=${g_projectDir}/04-output
export g_utilityDir="${SCRITP_ROOT_DIR}/utility"
export g_buildConfigureDir="${g_buildRootDir}/${g_compileMode}"
export g_outputConfigureDir="${g_outputRootDir}/${g_compileMode}"

function downloadLame3_100IfNeeded() {
    # 库下载地址
    libDownloadUrl="https://udomain.dl.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz"
    # 库id,规则为${libName}-${libVersion}
    libId="lame-3.100"
    # lib压缩包文件路径
    libZipPath="${g_inputRootDir}/${libId}.tar.gz"
    sourceLibPath="${g_inputRootDir}/${libId}"
    downloadZipIfNeeded ${libDownloadUrl} ${libZipPath} ${sourceLibPath}
}

function cloneJsShellUtilityIfNeeded() {
    targetDir="${SCRITP_ROOT_DIR}/utility/JsShellUtility"
    isDirNotEmpty ${targetDir}
    [[ "$?" = "1" ]] && return
    mkdir -p $(dirname ${targetDir})
    cmd="git clone git@github.com:GikkiAres/JsShellUtility.git ${targetDir}"
    eval ${cmd}
}

# 判断文件夹是否存在且非空
function isDirNotEmpty() {
    dirPath=$1
    if [[ ! -e "${dirPath}" ]]; then
        echo "dir exists"
        return 0
    fi
    info=$(ls ${dirPath})
    if [[ "${info}" = "" ]]; then
        echo "dir is empty"
        return 0
    fi
    return 1
}

# 判断哪些平台需要编译.
# Y,表示编译.
# N,表示不编译.
# A,表示自动判断当前系统,当前系统为指定系统就编译.
function buildIfNeeded() {
    declare -i length=${#platformArray[@]}
    for ((i = 0; i < ${length}; i++)); do
        platform=${platformArray[i]}
        buildFlag=${valueArray[i]}
        echo "platform: ${platform},build: ${buildFlag}"
        if [[ "${buildFlag}" == "Y" ]]; then
            . ${SCRITP_ROOT_DIR}/${platform}/${platform}_manager.sh
            [[ $? != 0 ]] && echo "error" && exit
        elif [[ "${buildFlag}" == "A" ]]; then
            # 等于key == o
            currentPlatform=""
            if [[ $currentPlatform == $key ]]; then
                . ${SCRITP_ROOT_DIR}/${platform}/${platform}_manager.sh
            fi
        fi
    done
}

# 根据设置,对指定平台进行操作.
function build2() {
    # if [[ $(yq '.ios.arm64' ./config.yml) == $FLAG_YES ]]; then
    #     platform=ios
    #     . ${SCRITP_ROOT_DIR}/${platform}/${platform}_manager.sh
    # fi
    # if [[ $(yq '.localhost.x86_64' ./config.yml) == $FLAG_YES ]]; then
    #     . $SCRITP_ROOT_DIR/mac/mac_manager.sh
    # fi
    $SCRITP_ROOT_DIR/windows/windows_manager.sh
}

# 将项目克隆到指定路径.

function aaa() {
    echo "aaa"
}

# 检查库是否存在,没有就下载,进行解压
function main() {
    # 1 make sure lib dir exists.
    build2
}

function main2() {
    cloneJsShellUtilityIfNeeded
    # 导入downloadermanager
    . ./02_script/utility/JsShellUtility/DownloadManager-0.0.1.sh
    . ./02_script/utility/JsShellUtility/JsStringValidator-0.0.0.sh
    [[ $? != 0 ]] && exit
    _libArray=$(yq '.libArray.* | key' $g_projectDir/config.yml)
    libArray=($_libArray)
    for lib in ${libArray[@]}; do
        #判断是否已经存在.
        [[ -e $g_inputRootDir/$lib ]] && echo "existed" && break
        url=$(yq ".libArray.$lib.src.url" $g_configFile)
        isStringValidUrl $url
        if [[ $? == 0 ]]; then
            version=$(yq ".libArray.$lib.version" $g_configFile)
            libId=$lib-$version
            zipPath=$g_inputRootDir/$libId.tar.gz
            dirPath=$g_inputRootDir/$libId
            echo "fetching $libId at $url"
            downloadZipToPath $url $zipPath $dirPath
            echo "$libId"
        fi
    done

    ffmpegPath=$(yq '.ffmpegPath' ./config.yml)
    cloneProjectToPath $ffmpegPath ./01_input/ffmpeg-6.0
    [[ $? != 0 ]] && exit

    # sdlPath=`yq '.sdlPath' ./config.yml`
    # cloneProjectToPath $sdlPath ./01_input/sdl2-2.0.10-ugc
    # [[ $? != 0 ]] && exit

    # x264Path=`yq '.x264Path' ./config.yml`
    # downloadZipToPath $x264Path ./01_input/x264.tar.bz2 ./01_input/x264
    # [[ $? != 0 ]] && exit

    build2
}
. $SCRITP_ROOT_DIR/common/common_build.sh
main
